﻿
var ico_Minus = "../Images/ico_Minus.gif";
var ico_plus = "../Images/ico_Plus.gif";
var ico_delete = "../Images/btnDelete.jpg";
//保存用箱信息表


$(document).ready(function () {
    //折叠面板代码
    $(".CollapseBar").bind("click", function () {
        var $collapseButton = $(this).find("img:first");
        var $panelBody = $(this).next("div:first");

        if ($panelBody.css("display") == "none") {
            $collapseButton.attr("src", ico_Minus);
        }
        else {
            $collapseButton.attr("src", ico_plus);
        }

        $panelBody.animate({ height: 'toggle', opacity: 'toggle' }, "slow");
    })

    //联系人信息折叠控制代码
    $("#hrefFaHuoRenMoreInfo").bind("click", function () {
        $("#trFaHuoRenContact").toggle();
        $("#trFaHuoRenSex").toggle();
        $("#trFaHuoRenFax").toggle();
        $("#trFaHuoRenEmail").toggle();
        return false;

    })

    $("#hrefShouHuoRenMoreInfo").bind("click", function () {
        $("#trShouHuoRenContact").toggle();
        $("#trShouHuoRenSex").toggle();
        $("#trShouHuoRenFax").toggle();
        $("#trShouHuoRenEmail").toggle();
        return false;
    })

    $("#hrefTongZhiRenMoreInfo").bind("click", function () {
        $("#trTongZhiRenContact").toggle();
        $("#trTongZhiRenSex").toggle();
        $("#trTongZhiRenFax").toggle();
        $("#trTongZhiRenEmail").toggle();
        return false;
    })

    $("#hrefDaiLiRenMoreInfo").bind("click", function () {
        $("#trDaiLiRenContact").toggle();
        $("#trDaiLiRenSex").toggle();
        $("#trDaiLiRenFax").toggle();
        $("#trDaiLiRenEmail").toggle();
        return false;
    })

    $("#hrefTongZhiRen2rdMoreInfo").bind("click", function () {
        $("#trTongZhiRen2rdContact").toggle();
        $("#trTongZhiRen2rdSex").toggle();
        $("#trTongZhiRen2rdFax").toggle();
        $("#trTongZhiRen2rdEmail").toggle();
        return false;
    })


    //绑定点击加载联系人对话框　　　　
    $(".imgFaHuoRenSearch").bind("click", { contactType: 1 }, LoadContactsIFrame);
    $(".imgShouHuoRenSearch").bind("click", { contactType: 2 }, LoadContactsIFrame);
    $(".imgTongZhiRenSearch").bind("click", { contactType: 3 }, LoadContactsIFrame);
    $(".imgDaiLiRenSearch").bind("click", { contactType: 4 }, LoadContactsIFrame);
    $(".imgTongZhiRen2rdSearch").bind("click", { contactType: 5 }, LoadContactsIFrame);


    //同订舱人和同发货人绑定
    $("#hrefOptTongDingCangRen").bind("click", { contactType: 1 }, AjaxLoadBookingUserInfo);
    $("#hrefOptTongDingCangRen1").bind("click", { contactType: 4 }, AjaxLoadBookingUserInfo);
    $("#hrefOptTongFaHuoRen").bind("click", { contactType: 3 }, SameAsFaHuoRen);
    $("#hrefOptTongFaHuoRen1").bind("click", { contactType: 4 }, SameAsFaHuoRen);
    $("#hrefOptTongShouHuoRen").bind("click", { contactType: 3 }, SameAsShouHuoRen);

     

    //AJAX 异步执行的LOADING显示设置
    jQuery.fn.center = function () {
        this.css("position", "absolute");
        this.css("top", ($(window).height() - this.height()) / 2 + $(window).scrollTop() + "px");
        this.css("left", ($(window).width() - this.width()) / 2 + $(window).scrollLeft() + "px");
        return this;
    }

    $("#divLoading").ajaxStart(function () {
        $(this).center();
        $(this).show();

    }).ajaxStop(function () {
        $(this).center();
        $(this).hide();
    });
})

//点击[同订舱人的时候，加载订舱人信息] 
function AjaxLoadBookingUserInfo(event)
{
    var contactType = event.data.contactType;
   
    var postData = "{userID:" + LoggedUserID + "}";
    
     $.ajax({
        type: "POST",
        contentType: "application/json",
        url: "../AjaxCallbackService.asmx/GetBookingUserInfo",
        data: postData,
        dataType: 'json',
        success: function(result) {
            $(result.d).each(function() {
                
                var contactInfo = new Array();
                contactInfo[0] = this["CompanyName"];
                contactInfo[1] = this["Address"];
                contactInfo[2] = this["PhoneNumber"];
                contactInfo[3] = this["RealName"];
                contactInfo[4] = this["Sex"];
                contactInfo[5] = this["Fax"];
                contactInfo[6] = this["Email"];
                
                SetContactsValue(contactType, contactInfo);
            });
        },
        error: function(httpException) {
        alert("数据加载失败，请重试-。");
        },
        timeout: 40000
    });
    
    return false;
} 

//开启AJAX异步加载联系人信息
function AjaxLoadContactInfo(event)
{
    var contactID  = $(this).val();
    var contactType = event.data.contactType;
    var postData = "{contactID:" + contactID + "}";
    
     $.ajax({
        type: "POST",
        contentType: "application/json",
        url: "../AjaxCallbackService.asmx/GetBookingContact",
        data: postData,
        dataType: 'json',
        success: function(result) {
            $(result.d).each(function() {
                
                var contactInfo = new Array();
                contactInfo[0] = this["Name"];
                contactInfo[1] = this["Address"];
                contactInfo[2] = this["Phone"];
                contactInfo[3] = this["Contact"];
                contactInfo[4] = this["Sex"];
                contactInfo[5] = this["Fax"];
                contactInfo[6] = this["Email"];
                
                SetContactsValue(contactType, contactInfo);
            });
        },
        error: function(httpException) {
        alert("数据加载失败，请重试。");
        },
        timeout: 40000
    });
}
function CreateContactsDialog()
{
    $('#divLoadContacts').dialog('destroy');
     //初始化联系人弹出对话框
    $('#divLoadContacts').dialog({ 
        title: "选择联系人",
        autoOpen: false,
        bgiframe: true,
        width:600,
        height:500,
        buttons: {
            "确定":ContactsIFrameCallback,
            "取消": function() 
            {
                $(this).dialog("close");
            }} 
    });
}

 

//加载联系人信息的回调函数
function ContactsIFrameCallback(iframeWin, topWin)
{
    var obj = $('#ContactsIFrame').contents().find(".tbAutoWrap tr.Selected span")
    if (obj.length ==0)
    {
        return;
    }
    
    var contactInfo = new Array();
    contactInfo[0] = obj[0].innerHTML;
    contactInfo[1] = obj[1].innerHTML;
    contactInfo[2] = obj[2].innerHTML;
    contactInfo[3] = obj[3].innerHTML;
    contactInfo[4] = obj[4].innerHTML;
    contactInfo[5] = obj[5].innerHTML;
    contactInfo[6] = obj[6].innerHTML;
    
    var contactType = $('#ContactsIFrame').contents().find(".lblHidContactType")[0].innerHTML;
    SetContactsValue(contactType, contactInfo);
    
    $('#divLoadContacts').dialog('close');
}

 

//动态增加PONO功能操作
$(document).ready(function()
{
    $("#hrefAddPONO").bind("click", function()
    {
        InitPonoData("");
        return false;
    })
})

function InitPonoData(value)
{
    var rowCount = $("#tbPONO tr").length;
    var trID = "trPONO" + rowCount;
    var inputID = "ctl00$ContentPlaceHolder1$txtPONO";
    var imgID = "img_txtPONO" + rowCount;
    
    var inputHTML = "<input type='text'class='txtPONO' maxlength='50' style='width: 380px;' id='" + inputID + "' name='" + inputID  + "' value='" + value + "'/>";
    var imgHTML = "<img id='" + imgID + "' src='" + ico_delete + "' style='cursor:pointer' />";
    var trHtml = "<tr id='" + trID + "'><td></td><td></td><td>" + inputHTML + "</td><td>"  + imgHTML + "</td></tr>";
    $("#tbPONO").append(trHtml);

    $("#"+ imgID).bind("click", function(){ if(confirm("您确实要删除吗？")) { $("#"+ trID).remove(); return false;} });
}

 

//历史URL返回
function HistoryBack(targetUrl)
{
    window.parent.BookingContent.location.href=targetUrl;
}

//打开保存模板对话框
function CreateSaveTemplateDialog()
{
    $('#divSaveTemplate').dialog('destroy');
     //初始化联系人弹出对话框
    $('#divSaveTemplate').dialog({ 
        title: "保存模板",
        autoOpen: false,
        bgiframe: true,
        width:450,
        height:170
    });
    $("#divSaveTemplate").parent().appendTo("#JQueryDialogPaernt");
}


//保存模板弹出对话框
function OpenSaveTemplateDlg()
{ 
    //清空模板名称框
    $(".txtDlgTemplateName").val("");
    $(".lblDlgErrorMsg").text("");
    CreateSaveTemplateDialog();
    $('#divSaveTemplate').dialog('open');
    return false;
}
 
function DevideCarton(obj)
{
    var cartonController = new CartonFormController(pageCtrlPrefix);
    cartonController.InitInstance();
    
    var index = $(obj).closest("table").attr("index");
    cartonController.DivideCarton(index);
}

function SetCheckValue(obj, hidID)
{
    if ($(obj).attr("checked"))
    {
        $(obj).siblings("#" + pageCtrlPrefix + hidID).val("Y");       
    }
    else 
    {
        $(obj).siblings("#" + pageCtrlPrefix + hidID).val("N");
    }
}

//当处于编辑状态时，调用此方法，发起AJAX请求，初始化新的表单。
function InitAllCartonTable()
{
    var cartonController = new CartonFormController(pageCtrlPrefix);
    cartonController.InitInstance();
    
    //遍历全局数组 cartonTableList又服务器端注册入客户端。
    for (var i = 0; i < cartonTableList.length; i++) {
        var index = cartonController.AddNewCarton(true);
        cartonController.AjaxLoadCartonData(index, cartonTableList[i]);
    }

}
//当处于编辑状态时，调用此方法，发起AJAX请求，初始化新的表单。
function InitAllCartonTable_bak() {
    var cartonController = new CartonFormController(pageCtrlPrefix);
    cartonController.InitInstance();

    //遍历全局数组 cartonTableList又服务器端注册入客户端。
    for (var i = 0; i < cartonTableList.length; i++) {
        var index = cartonController.AddNewCarton_bak(true);
        cartonController.AjaxLoadCartonData(index, cartonTableList[i]);
    }

}
//当处于编辑状态时，调用此方法，发起AJAX请求，初始化新的表单。
function InitAllCartonTable_fx() {
    var cartonController = new CartonFormController(pageCtrlPrefix);
    cartonController.InitInstance();

    //遍历全局数组 cartonTableList又服务器端注册入客户端。

    for (var i = 0; i < cartonTableList.length; i++) {
        var index = cartonController.AddNewCarton_bak(true);
        cartonController.AjaxLoadCartonData_fx(index, cartonTableList[i]);
    }
    //cartonController.ApportionJZC();   
}

//用箱信息表单控件定义
CartonControl = function(ID, ModelProperty, ControlType, IsRequired, RegExp) 
{
    this.ID = ID; //控件ID
    this.ModelProperty = ModelProperty; //对应实体类的属性名称
    this.ControlType = ControlType;     //控件类型 Val:值类型控件，Check：CheckBox类型控件, Select:DropDownList类型控件
    this.IsRequired = IsRequired;       //是否必填属性
    this.RegExp = RegExp;               //正则表达式验证
}

CartonFormController = function(ControlPrefix) 
{
    //控件组
    this.ControlList = new Array();
    
     //控件前缀     
    this.ControlPrefix = ControlPrefix == undefined? "" : ControlPrefix;    
}

//初始化控件映射表 
CartonFormController.prototype.InitInstance = function()
{
    this.ControlList[0] = this.ControlList["txtYxXiangXing"] = new CartonControl("txtYxXiangXing", "ctnclcd", "Val");
    this.ControlList[1] = this.ControlList["txtYxXiangShu"] = new  CartonControl("txtYxXiangShu", "ctnquan", "Val");
    this.ControlList[2] = this.ControlList["txtYxXiangHao"] = new  CartonControl("txtYxXiangHao", "cartonno", "Val");
    this.ControlList[3] = this.ControlList["txtYxFengHao"] = new  CartonControl("txtYxFengHao", "sealno", "Val");
    this.ControlList[4] = this.ControlList["txtYxBaoZhuangMingCheng"] = new CartonControl("txtYxBaoZhuangMingCheng", "ctnr_unit", "Val");
    this.ControlList[5] = this.ControlList["txtYxJianShu"] = new  CartonControl("txtYxJianShu", "ctnr_sum", "Val");
    this.ControlList[6] = this.ControlList["txtYxMaoZhong"] = new  CartonControl("txtYxMaoZhong", "gross_weight", "Val");
    this.ControlList[7] = this.ControlList["txtYxChiMa"] = new  CartonControl("txtYxChiMa", "measurement", "Val");
    this.ControlList[8] = this.ControlList["txtYxPinMing"] = new  CartonControl("txtYxPinMing", "description", "Val");
    this.ControlList[9] = this.ControlList["chkYxIsDanChengXiang"] = new  CartonControl("chkYxIsDanChengXiang", "onewaybox", "Check");
    this.ControlList[10] = this.ControlList["chkYxIsShiPinXiang"] = new  CartonControl("chkYxIsShiPinXiang", "foodbox", "Check");
    this.ControlList[11] = this.ControlList["chkYxIsXinXiang"] = new  CartonControl("chkYxIsXinXiang", "newbox", "Check");    
    this.ControlList[12] = this.ControlList["txtYxWenDu"] = new  CartonControl("txtYxWenDu", "tempe", "Val");
    this.ControlList[13] = this.ControlList["txtYxShiDu"] = new  CartonControl("txtYxShiDu", "humi", "Val");
    this.ControlList[14] = this.ControlList["txtYxTongFengDu"] = new  CartonControl("txtYxTongFengDu", "vent", "Val");
    this.ControlList[15] = this.ControlList["txtYxChang"] = new  CartonControl("txtYxChang", "len", "Val");
    this.ControlList[16] = this.ControlList["txtYxKuan"] = new  CartonControl("txtYxKuan", "width", "Val");
    this.ControlList[17] = this.ControlList["txtYxGao"] = new  CartonControl("txtYxGao", "height", "Val");
    this.ControlList[18] = this.ControlList["txtYxTeShuYaoQiu"] = new  CartonControl("txtYxTeShuYaoQiu", "note", "Val");
    this.ControlList[19] = this.ControlList["hidYxIsDanChengXiang"] = new  CartonControl("hidYxIsDanChengXiang", "onewaybox", "Val");
    this.ControlList[20] = this.ControlList["hidYxIsShiPinXiang"] = new  CartonControl("hidYxIsShiPinXiang", "foodbox", "Val");
    this.ControlList[21] = this.ControlList["hidYxIsXinXiang"] = new CartonControl("hidYxIsXinXiang", "newbox", "Val");
    this.ControlList[22] = this.ControlList["txtYxHaiGuanBianMaOne"] = new CartonControl("txtYxHaiGuanBianMaOne", "hscode", "Val");
}

DeleteCartonTable = function(obj)
{
    if (confirm("确实要删除这项用箱信息吗？"))
    {
        index = $(obj).closest("table").remove();
    }
}

//创建新的用箱信息表单
CartonFormController.prototype.AddNewCarton = function (IsClone) {
    var maxIndex = this.GetCartonMaxIndex();
    var newIndex = maxIndex + 1;
    var tableObj = $("#tbCartonTable:first").clone(true); //复制第一个表单
    tableObj.attr("index", newIndex);

    var deleteCartonHtml = "<tr><td></td><td></td><td><a id='hrefDeleteCarton' href='javascript:void(0);' onclick='DeleteCartonTable(this);'>";
    deleteCartonHtml = deleteCartonHtml + "<span style='float: left; padding-left:5px'>[删除]</span></a></td></tr>";

    var html = "<hr />";
    tableObj.append(deleteCartonHtml);
    $("#tbCartonContainer").append(html);
    $("#tbCartonContainer").append(tableObj);             //将表单追加在最后

    if (!IsClone)   //如果不是完整克隆，则清空表单数据
    {
        this.SetCartonModel(newIndex, new Array());
    }
    return newIndex;
}
CartonFormController.prototype.AddNewCarton_bak = function (IsClone) {
    var maxIndex = this.GetCartonMaxIndex();
    var newIndex = maxIndex + 1;
    var tableObj = $("#tbCartonTable:first").clone(true); //复制第一个表单
    tableObj.attr("index", newIndex);

    var deleteCartonHtml = "<tr><td></td><td></td><td><a id='hrefDeleteCarton' href='javascript:void(0);' onclick='DeleteCartonTable(this);'>";
    deleteCartonHtml = deleteCartonHtml + "<span style='float: left; padding-left:5px'>[删除]</span></a></td></tr>";

    var html = "<hr />";
    //tableObj.append(deleteCartonHtml);
    $("#tbCartonContainer").append(html);
    $("#tbCartonContainer").append(tableObj);             //将表单追加在最后

    if (!IsClone)   //如果不是完整克隆，则清空表单数据
    {
        this.SetCartonModel(newIndex, new Array());
    }
    return newIndex;
}

//在指定的位置插入用箱表单
CartonFormController.prototype.InsertNewCarton = function(InsertAfterIndex, IsClone)
{
    var maxIndex = this.GetCartonMaxIndex();
    var newIndex = maxIndex + 1;;
    var tableObj = $(".clsCartonTable[index='" + InsertAfterIndex + "']").clone(true); //复制指定位置的表单
    tableObj.attr("index", newIndex);
//    if (tableObj.find("#hrefDeleteCarton").length == 0)
//    {
//        var deleteCartonHtml = "<a id='hrefDeleteCarton' href='javascript:void(0);' onclick='DeleteCartonTable(this);'>";
//        deleteCartonHtml = deleteCartonHtml + "<span style='float: lef; padding-left:5px'>[删除]</span></a>";
//        tableObj.find("#tdToolbar").append(deleteCartonHtml);
//    }
   
    $(".clsCartonTable[index='" + InsertAfterIndex + "']").after(tableObj);
       
    if (!IsClone)   //如果不是完整克隆，则清空表单数据
    {
        this.SetCartonModel(newIndex, new Array());
    }
    
    return newIndex;
}


//为用箱表单设置值
CartonFormController.prototype.SetCartonModel = function (index, carton) {
    var $cartonTable = $(".clsCartonTable[index='" + index + "']");
    for (var i = 0; i < this.ControlList.length; i++) {
        var ControlID = this.ControlList[i].ID;
        var ControlType = this.ControlList[i].ControlType;
        var ModelProperty = this.ControlList[i].ModelProperty;
        if (ControlType == "Val") //值类型控件
        {
            var value = carton[ModelProperty] == undefined ? "" : carton[ModelProperty];
            $cartonTable.find("#" + this.ControlPrefix + ControlID).val(value);
        }
        if (ControlType == "Select") {
            var value = carton[ModelProperty] == undefined ? "0" : carton[ModelProperty];
            $cartonTable.find("#" + this.ControlPrefix + ControlID).val(value);
        }
        if (ControlType == "Check") //CheckBox控件
        {
            var value = false;
            if (carton[ModelProperty] == undefined) {
                value = false;
            }
            else {
                value = carton[ModelProperty] == "Y" ?true: false;
            }

            $cartonTable.find("#" + this.ControlPrefix + ControlID).attr("checked", value);
        }
    }
}

//将表单值转换为一个实体对象
CartonFormController.prototype.GetCartonModel = function(index)
{
    var carton = new Array();
    var $cartonTable = $(".clsCartonTable[index='" + index + "']");
    
     for(var i=0; i<this.ControlList.length; i++)
    {
        var ControlID = this.ControlList[i].ID;
        var ControlType = this.ControlList[i].ControlType;
        var ModelProperty = this.ControlList[i].ModelProperty;
        
        if (ControlType == "Val") //值类型控件
        {
            carton[ModelProperty] = $cartonTable.find("#" + this.ControlPrefix +  ControlID).val();
        }
        if (ControlType == "Select")
        {
            carton[ModelProperty] = $cartonTable.find("#" + this.ControlPrefix +  ControlID).val();
        }
        if (ControlType == "Check") //CheckBox控件
        {
            var checked = $cartonTable.find("#" + this.ControlPrefix + ControlID).attr("checked");
            carton[ModelProperty] = checked ? "Y" : "N";
        }
    }
    return carton;
}


//获取用箱表单的最大索引
CartonFormController.prototype.GetCartonMaxIndex = function()
{
    var $cartons = $(".clsCartonTable");
    var maxIndex = parseInt( $($cartons[0]).attr("index") );
    for(var i=0; i<$cartons.length; i++)
    {
        var index = parseInt( $($cartons[i]).attr("index") );
        if (index > maxIndex)
        {
            maxIndex = index;
        }
    }
    return maxIndex;
}

//使用AJAX从服务器异步加载用箱数据
CartonFormController.prototype.AjaxLoadCartonData = function(index, cartonID)
{
    var controller = this;
    var postData = "{cartonID:" + cartonID + "}";
    $.ajax({
        type: "POST",
        contentType: "application/json",
        url: "../AjaxCallbackService.asmx/GetBookingCarton",
        data: postData,
        dataType: 'json',
        success: function(result) 
        {
           controller.SetCartonModel(index, result.d[0]);
        },
        error: function(httpException) {
            alert("数据加载失败，请重试--。");
        },
       timeout: 40000
    })
}

//使用AJAX从服务器异步加载用箱数据

CartonFormController.prototype.AjaxLoadCartonData_fx = function (index, cartonID) {
    var controller = this;
    var postData = "{cartonID:" + cartonID + "}";
    $.ajax({
        type: "POST",
        contentType: "application/json",
        url: "../AjaxCallbackService.asmx/GetBookingCarton_fx",
        data: postData,
        dataType: 'json',
        //async:false,
        success: function (result) {
            controller.SetCartonModel(index, result.d[0]);
        },
        error: function (httpException) {
            alert("数据加载失败，请重试---。");
        },
        timeout: 40000
    })
}


//从CARTON 表单中寻找内部控件
CartonFormController.prototype.FindControl = function(index,id)
{
    var $control;
    var $cartonTable = $(".clsCartonTable[index='" + index + "']");
    return $cartonTable.find("#" + this.ControlPrefix + this.ControlList[id].ID)
}

//将一条用箱信息拆分为多条
CartonFormController.prototype.DivideCarton = function(index)
{
    var $txtYxXiangShu = this.FindControl(index, "txtYxXiangShu");
    var txtYxXiangShu = $.trim($txtYxXiangShu.val());
    var $txtYxJianShu = this.FindControl(index, "txtYxJianShu");
    var txtYxJianShu = $.trim($txtYxJianShu.val());
    var $txtYxMaoZhong = this.FindControl(index, "txtYxMaoZhong");
    var txtYxMaoZhong = $.trim($txtYxMaoZhong.val());
    var $txtYxChiMa = this.FindControl(index, "txtYxChiMa");
    var txtYxChiMa = $.trim($txtYxChiMa.val());
    if (txtYxXiangShu == "")
    {
        alert("箱数为空，不能进行拆分。");
        $txtYxXiangShu.focus();
        return false;
    }
    if (!IsPosInt(txtYxXiangShu))
    {
        alert("请输入正确的用箱信息[箱数]。");
        $txtYxXiangShu.focus();
        return false;
    }
    if (txtYxXiangShu == "1")
    {
        alert("用箱信息[箱数]等于(1),不能再拆分。");
        $txtYxXiangShu.focus();
        return false;
    }
    if (txtYxJianShu == "" && txtYxMaoZhong=="" && txtYxChiMa =="")
    {
        alert("请至少输入用箱信息[件数、毛重、尺码]的一项或全部。");
        $txtYxXiangShu.focus();
        return false;
    }
    //件数
    if (txtYxJianShu != "")
    {
        if (!IsPosInt(txtYxJianShu))
        {
            $txtYxJianShu.focus();
            alert("请输入正确的用箱信息[件数]。");
            return false;
        }
    }
    if (txtYxMaoZhong != "")
    {
        if (!IsPosRealNum(txtYxMaoZhong))
        {
            $txtYxMaoZhong.focus();
            alert("请输入正确的用箱信息[毛重]。");
            return false;
        }
    }
    if (txtYxChiMa != "")
    {
        if (!IsPosRealNum(txtYxChiMa))
        {
            $txtYxChiMa.focus();
            alert("请输入正确的用箱信息[尺码]。");
            return false;
        }
    }
        
    var carton = this.GetCartonModel(index);
     //清空箱号，封号
    carton["cartonno"]= "";
    carton["sealno"] = "";
    carton["ctnquan"] = 1; //设置箱数为1
    
    
    var XiangShu = parseInt(txtYxXiangShu);
    var JianShu = isNaN(parseInt(txtYxJianShu))? 0 : parseFloat(txtYxJianShu);
    var MaoZhong = isNaN(parseFloat(txtYxMaoZhong))? 0 : parseFloat(txtYxMaoZhong);
    var ChiMa = isNaN(parseFloat(txtYxChiMa))? 0 : parseFloat(txtYxChiMa);
    
    carton["ctnr_sum"] = parseInt(JianShu / XiangShu);
    carton["gross_weight"] = parseFloat(MaoZhong / XiangShu).toFixed(3);
    carton["measurement"] = parseFloat(ChiMa / XiangShu).toFixed(3);  
    this.SetCartonModel(index, carton);
    
    for(var i=0; i<XiangShu-1; i++)
    {
        var newIndex = this.InsertNewCarton(index, true);
        
        this.SetCartonModel(newIndex, carton);
    }  
    
    return true;
}

//从货物信息分摊件重尺到用箱信息
CartonFormController.prototype.ApportionJZC = function () {
    var $txtJianShu = $($(".txtJianShu")[0]);
    var $txtMaoZhong = $($(".txtMaoZhong")[0]);
    var $txtChiMa = $($(".txtChiMa")[0]);

    var TotalJianShu = $.trim($txtJianShu.val());
    var TotalMaoZhong = $.trim($txtMaoZhong.val());
    var TotalChiMa = $.trim($txtChiMa.val());
    //进行数值校验
    if (TotalJianShu == '') {
        alert("请输入货物信息[件数]。")
        $txtJianShu.focus();
        return false;
    }
    else {
        if (!IsPosInt(TotalJianShu)) {
            alert("请输入正确的货物信息[件数]。");
            $txtJianShu.focus();
            return false;
        }
    }

    if (TotalMaoZhong != "") {
        if (!IsPosRealNum(TotalMaoZhong)) {
            alert("请输入正确的货物信息[毛重]。");
            $txtMaoZhong.focus();
            return false;
        }
    }

    if (TotalChiMa != "") {
        if (!IsPosRealNum(TotalChiMa)) {
            alert("请输入正确的货物信息[尺码]。");
            $txtChiMa.focus();
            return false;
        }
    }

    var TotalXiangShu = 0;
    var $YxXiangShu = $(".txtYxXiangShu");
    var $YxJianShu = $(".txtYxJianShu");
    var $YxMaoZhong = $(".txtYxMaoZhong");
    var $YxChiMa = $(".txtYxChiMa");

    //得到总箱数
    for (var i = 0; i < $YxXiangShu.length; i++) {
        var XiangShu = $.trim($($YxXiangShu[i]).val());
        if (XiangShu == '') {
            alert("请输入用箱信息[箱数]。");
            $($YxXiangShu[i]).focus();
            return false;
        }
        if (!IsPosInt(XiangShu)) {
            alert("请输入正确的用箱信息[箱数]。");
            $($YxXiangShu[i]).focus();
            return false;
        }
        TotalXiangShu = TotalXiangShu + parseInt(XiangShu);
    }

    if (TotalXiangShu > TotalJianShu) {
        alert("用箱信息总[箱数](" + TotalXiangShu + ")大于货物信息[件数](" + TotalJianShu + ")，无法完成计算。");
        return false;
    }
    for (var i = 0; i < $YxXiangShu.length; i++) {

        try {
            var XiangShu = parseInt($.trim($($YxXiangShu[i]).val()));

            var UnitJianShu = XiangShu == 0 ? 0 : TotalJianShu / TotalXiangShu;
            var UnitMaoZhong = XiangShu == 0 ? 0 : TotalMaoZhong / TotalXiangShu;
            var UnitChiMa = XiangShu == 0 ? 0 : TotalChiMa / TotalXiangShu

            //单箱数据 
            $($YxJianShu[i]).val(parseInt(UnitJianShu))
            $($YxMaoZhong[i]).val(parseFloat(UnitMaoZhong).toFixed(3));
            $($YxChiMa[i]).val(parseFloat(UnitChiMa).toFixed(3));
        }
        catch (e) {
            //do nothing.......
        }
    }
}

//检验用箱信息合计和货物信息对比是否正确
CartonFormController.prototype.ComputeCartonNOP = function () {

    var $txtJianShu = $($(".txtJianShu")[0]);
    var $txtMaoZhong = $($(".txtMaoZhong")[0]);
    var $txtChiMa = $($(".txtChiMa")[0]);

    var TotalJianShu = $.trim($txtJianShu.val());
    var TotalMaoZhong = $.trim($txtMaoZhong.val());
    var TotalChiMa = $.trim($txtChiMa.val());

    //检查货物信息是否填写
    if (TotalJianShu == '' || TotalMaoZhong == "" || TotalChiMa == "") {
        return true;
    }

    var TotalXiangShu = 0;
    var $YxXiangShu = $(".txtYxXiangShu");
    var $YxJianShu = $(".txtYxJianShu");
    var $YxMaoZhong = $(".txtYxMaoZhong");
    var $YxChiMa = $(".txtYxChiMa");
    var TotalYxJianShu = 0;
    var TotalYxMaoZhong = 0;
    var TotalYxChiMa = 0;

    for (var i = 0; i < $YxXiangShu.length; i++) {
        try {
            var UnitXiangShu = $.trim($($YxXiangShu[i]).val());
            var UnitJianShu = $.trim($($YxJianShu[i]).val());
            var UnintMaoZhong = $.trim($($YxMaoZhong[i]).val());
            var UnitChiMa = $.trim($($YxChiMa[i]).val());

            if (UnitXiangShu == "" || UnitJianShu == "" || UnintMaoZhong == "" || UnitChiMa == "") {
                return true;
            }
            //单箱数据  
            TotalYxJianShu = TotalYxJianShu + parseInt(UnitJianShu * UnitXiangShu);
            TotalYxMaoZhong = TotalYxMaoZhong + parseFloat(UnintMaoZhong * UnitXiangShu);
            TotalYxChiMa = TotalYxChiMa + parseFloat(UnitChiMa * UnitXiangShu); 
        }
        catch (e) {
            return true;
        }
    }

    TotalYxJianShu = parseInt(TotalYxJianShu);
    alert(TotalYxJianShu);
    TotalYxMaoZhong = parseFloat(TotalYxMaoZhong).toFixed(3);
    TotalYxChiMa = parseFloat(TotalYxChiMa).toFixed(3);

    if (TotalYxJianShu != parseInt(TotalJianShu)) {
        alert("货物信息件数(" + parseInt(TotalJianShu) + ")不等于用箱信息总件数(" + TotalYxJianShu + ")，请检查输入的数据。");
        return false;
    }
    if (TotalYxMaoZhong != parseFloat(TotalMaoZhong).toFixed(3)) {
        alert("货物信息毛重(" + parseFloat(TotalMaoZhong).toFixed(3) + ")不等于用箱信息总毛重(" + TotalYxMaoZhong + ")，请检查输入的数据。");
        return false;
    }
    if (TotalYxChiMa != parseFloat(TotalChiMa).toFixed(3)) {
        alert("货物信息尺码(" + parseFloat(TotalChiMa).toFixed(3) + ")不等于用箱信息总尺码(" + TotalYxChiMa + ")，请检查输入的数据。");
        return false;
    }

    return true;
}

//将合计转换为英文描述
function ComputeTotalNOP()
{
    var hejiValue = "SAY: ";
    var $txtJianShu = $("#" + pageCtrlPrefix + "txtJianShu");
    var $txtBaoZhuangMingCheng = $("#" + pageCtrlPrefix + "txtBaoZhuangMingCheng");
    
    if ($.trim($txtJianShu.val()) != '')
    {
        hejiValue = hejiValue + DigitNumToEngNum($txtJianShu.val())
        if ($txtBaoZhuangMingCheng.val() != "0")
        {
            hejiValue = hejiValue + " " + $txtBaoZhuangMingCheng.text();
        }
        hejiValue = hejiValue + " ONLY.";
    }
   
    $("#" + pageCtrlPrefix + "txtXiangShuHeJi").val(hejiValue);
}

var divAjaxAutoBoxClass1 = "divAjaxAutoBox";
var tbAjaxDataBoxClass1 = "tbAjaxDataBox";
var divAjaxAutoBoxPrefix2 = "jAjax_divAutoBox_2";
var tbAjaxDataBoxPrefix2 = "jAjax_tbDataBox_2";


